﻿using System.Collections.ObjectModel;
using Samurai.Infrastructure;

namespace Samurai.Client.DataContexts
{
    public class RepositoryDataContext  : ADataContext
    {
        private static RepositoryDataContext _instance;
        public static RepositoryDataContext Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new RepositoryDataContext();
                return _instance;
            }
        }

        private RepositoryDataContext()
        {
            Repositories = new ObservableCollection<Repository>();

            Repositories.CollectionChanged += Repositories_CollectionChanged;

            AddToRepositoryCommand = new AddToRepositoryCommand(Repositories);
            DeleteFromRepositoryCommand = new DeleteFromRepositoryCommand(Repositories);

            RefreshRepositoriesCommand = new RefreshRepositoriesCommand(Repositories);
        }

        static void Repositories_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                CrawlDirectoryCommand.Instance.Execute(null);
            }
        }

        public override void OnLoaded()
        {
            RefreshRepositoriesCommand.Execute(null);
        }

        public ObservableCollection<Repository> Repositories { get; set; }

        public AddToRepositoryCommand AddToRepositoryCommand { get; set; }

        public DeleteFromRepositoryCommand DeleteFromRepositoryCommand { get; set; }

        public RefreshRepositoriesCommand RefreshRepositoriesCommand { get; set; }
    }
}